package com.hnu.store.controller;

import com.hnu.store.service.ex.CaptchaException;
import com.hnu.store.service.ex.CaseDeleteException;
import com.hnu.store.service.ex.CaseInsertException;
import com.hnu.store.service.ex.CaseUpdateException;
import com.hnu.store.service.ex.CertifyException;
import com.hnu.store.service.ex.ChangeInfoException;
import com.hnu.store.service.ex.ChangeSaleException;
import com.hnu.store.service.ex.DeleteNftException;
import com.hnu.store.service.ex.EditNftException;
import com.hnu.store.service.ex.EmailException;
import com.hnu.store.service.ex.FeedbackAddReplyException;
import com.hnu.store.service.ex.FeedbackInsertException;
import com.hnu.store.service.ex.FileNotFoundException;
import com.hnu.store.service.ex.FileReceiveException;
import com.hnu.store.service.ex.FileTypeException;
import com.hnu.store.service.ex.GetCaseException;
import com.hnu.store.service.ex.GetNewsException;
import com.hnu.store.service.ex.InsertException;
import com.hnu.store.service.ex.NewsDeleteException;
import com.hnu.store.service.ex.NewsInsertException;
import com.hnu.store.service.ex.NewsUpdateException;
import com.hnu.store.service.ex.NftListException;
import com.hnu.store.service.ex.PasswordNotMatchException;
import com.hnu.store.service.ex.SearchRecordInsertException;
import com.hnu.store.service.ex.ServiceException;
import com.hnu.store.service.ex.TradeNotExistException;
import com.hnu.store.service.ex.UploadException;
import com.hnu.store.service.ex.UserNotExistException;
import com.hnu.store.service.ex.UsernameDuplicateException;
import com.hnu.store.service.ex.WorkNotExistException;
import com.hnu.store.utils.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 控制层的基类，用于统一异常处理
 */
public class BaseController {

  //  正常的状态码设置为200
  public static final int OK = 200;

  //  操作不成功的统一处理，需要定义一个方法
  @ExceptionHandler(ServiceException.class)

  /*
    @ExceptionHandler用于统一处理抛出的异常,参数是异常类的class实例类型
   修饰的方法会自动将异常传递给参数列表
   请求处理方法，返回值就是传递给前端的数据
   */

  public JsonResult handleException(Throwable e) {
    JsonResult result = new JsonResult(e);
    if (e instanceof UsernameDuplicateException) {
      System.out.println(e.getMessage());
      result.setCode(4000);
      result.setMessage("用户名被占用");
    } else if (e instanceof InsertException) {
      System.out.println(e.getMessage());
      result.setCode(5000);
      result.setMessage("注册未知异常");
    } else if (e instanceof EmailException) {
      System.out.println(e.getMessage());
      result.setCode(6000);
      result.setMessage("邮件发送异常");
    } else if (e instanceof CaptchaException) {
      System.out.println(e.getMessage());
      result.setCode(7000);
      result.setMessage("验证码错误");
    } else if (e instanceof UserNotExistException) {
      System.out.println(e.getMessage());
      result.setCode(8000);
      result.setMessage("用户不存在");
    } else if (e instanceof PasswordNotMatchException) {
      System.out.println(e.getMessage());
      result.setCode(9000);
      result.setMessage("密码错误");
    } else if (e instanceof ChangeInfoException) {
      System.out.println(e.getMessage());
      result.setCode(10000);
      result.setMessage("修改失败");
    } else if (e instanceof TradeNotExistException) {
      System.out.println(e.getMessage());
      result.setCode(11000);
      result.setMessage("该用户无交易信息");
    } else if (e instanceof NftListException) {
      System.out.println(e.getMessage());
      result.setCode(11100);
      result.setMessage("该用户无NFT信息");
    } else if (e instanceof UploadException) {
      System.out.println("文件上传异常");
      result.setCode(12000);
      result.setMessage("上传失败");
    } else if (e instanceof FileReceiveException) {
      System.out.println(e.getMessage());
      result.setCode(12100);
      result.setMessage("未上传文件");
    } else if (e instanceof WorkNotExistException) {
      System.out.println(e.getMessage());
      result.setCode(13000);
      result.setMessage("该NFT不存在");
    } else if (e instanceof DeleteNftException) {
      System.out.println(e.getMessage());
      result.setCode(13100);
      result.setMessage(e.getMessage());
    } else if (e instanceof EditNftException) {
      System.out.println(e.getMessage());
      result.setCode(13200);
      result.setMessage("编辑NFT失败");
    } else if (e instanceof FileNotFoundException) {
      System.out.println(e.getMessage());
      result.setCode(14000);
      result.setMessage("文件不存在");
    }  else if (e instanceof CaseInsertException){
      System.out.println(e.getMessage());
      result.setCode(99000);
      result.setMessage("添加案例失败");
    } else if (e instanceof CaseDeleteException){
      System.out.println(e.getMessage());
      result.setCode(98000);
      result.setMessage("删除案例失败");
    } else if (e instanceof CaseUpdateException){
      System.out.println(e.getMessage());
      result.setCode(97000);
      result.setMessage("编辑案例失败");
    } else if (e instanceof NewsInsertException){
      System.out.println(e.getMessage());
      result.setCode(96000);
      result.setMessage("添加新闻失败");
    } else if (e instanceof NewsDeleteException){
      System.out.println(e.getMessage());
      result.setCode(95000);
      result.setMessage("删除新闻失败");
    } else if (e instanceof NewsUpdateException){
      System.out.println(e.getMessage());
      result.setCode(94000);
      result.setMessage("编辑新闻失败");
    } else if (e instanceof SearchRecordInsertException){
      System.out.println(e.getMessage());
      result.setCode(93000);
      result.setMessage("搜索记录更新失败");
    } else if (e instanceof ChangeSaleException) {
      System.out.println(e.getMessage());
      result.setCode(16000);
      result.setMessage("售单编辑失败");
    } else if (e instanceof FeedbackInsertException) {
      result.setCode(15000);
      result.setMessage("插入反馈记录失败");
    } else if (e instanceof FeedbackAddReplyException) {
      result.setCode(15100);
      result.setMessage("插入反馈回复失败");
    } else if (e instanceof CertifyException) {
      result.setCode(17000);
      result.setMessage("实名认证失败");
    } else if (e instanceof GetNewsException) {
      result.setCode(18000);
      result.setMessage(e.getMessage());
    } else if (e instanceof GetCaseException) {
      result.setCode(18100);
      result.setMessage(e.getMessage());
    } else if (e instanceof FileTypeException) {
      result.setCode(19000);
      result.setMessage("请选择正确的文件类型");
    } else {
      System.out.println(e.getMessage());
      result.setCode(20000);
      result.setMessage("未知异常,请联系管理员");
    }
    //  result响应给前端
    return result;
  }
}
